package cn.dnui_zzh807.dnuifood_16110100807.controller;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.fragment.Fragment1;
import cn.dnui_zzh807.dnuifood_16110100807.fragment.Fragment2;
import cn.dnui_zzh807.dnuifood_16110100807.fragment.Fragment3;
import cn.dnui_zzh807.dnuifood_16110100807.fragment.Fragment4;

public class FragmentActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;

    private ViewPager viewPager;
    private List<Fragment> fragmentList;//添加那个五个Frangment
    private FragmentPagerAdapter fragmentPagerAdapter;//适配器

    private BottomNavigationView.OnNavigationItemSelectedListener naviListener;
    private ViewPager.OnPageChangeListener pageChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_interface);

      init();

    }

    private void init(){
        viewPager=findViewById(R.id.viewPager);
        bottomNavigationView=findViewById(R.id.bottomNavigationView);
        fragmentList = new ArrayList<>();
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        Fragment4 fragment4 = new Fragment4();
        fragmentList.add(fragment1);fragmentList.add(fragment2);fragmentList.add(fragment3);fragmentList.add(fragment4);

        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public android.support.v4.app.Fragment getItem(int position) {

                return fragmentList.get(position);//滑动效果
            }

            @Override
            public int getCount() {

                return fragmentList.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);


        naviListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id){
                    case R.id.sy:
                        viewPager.setCurrentItem(0);//当选中的是blog时 让viewpager选中第一页
                        break;
                    case R.id.sc:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.ss:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.wd:
                       viewPager.setCurrentItem(3);
                        break;
                }
                return true;//一定要返回true
            }
        };

        bottomNavigationView.setOnNavigationItemSelectedListener(naviListener);
        pageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                switch (position){
                    case 0:
                        bottomNavigationView.setSelectedItemId(R.id.sy);
                        break;
                    case 1:
                        bottomNavigationView.setSelectedItemId(R.id.sc);
                        break;
                    case 2:
                        bottomNavigationView.setSelectedItemId(R.id.ss);
                        break;
                    case 3:
                        bottomNavigationView.setSelectedItemId(R.id.wd);
                        break;

                }
            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        };
        viewPager.addOnPageChangeListener(pageChangeListener);



    }


}
